package cn.fl.project.service.impl;

import cmm.comm.facade.upload.constant.SysUlFilesRecConstant;
import cmm.mid.core.framework.page.DataResultVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.preloan.enums.LogBusinessTypeEnum;
import cn.fl.project.api.IPrjQuatPrpsZGApiService;
import cn.fl.project.constant.PrjPrdPaySchMConstant;
import cn.fl.project.service.IPrjQuatPrpsZGService;
import cn.fl.spl.utils.FileUtil;
import cn.fl.system.constant.SysLogConstant;
import com.lionbridge.log.ext.ExtSlf4jLogger;
import com.lionbridge.log.ext.ExtSlf4jLoggerBuilder;
import com.lionbridge.log.ext.LogTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileOutputStream;
import java.util.List;
import java.util.Map;

/**
 * @Description: 项目报价方案表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Slf4j
@Service
public class PrjQuatPrpsZGServiceImpl implements IPrjQuatPrpsZGService {
	private static ExtSlf4jLogger logger = ExtSlf4jLoggerBuilder.create()
			.setLogger(log).setBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setSubBusinessType(LogBusinessTypeEnum.QUOTATION_SCHEME.getCode())
			.setLogType(LogTypeEnum.BUSINESS).build().setKeywords("PrjQuatPrpsZG");
	@Autowired
	private IPrjQuatPrpsZGApiService prjQuatPrpsZGApiService;
	@Override
	public void modifyDealPFHistoryData() throws BaseException {
		prjQuatPrpsZGApiService.modifyDealPFHistoryData();

	}
	@Override
	public void modifyDealWSHistoryData() throws BaseException {
		prjQuatPrpsZGApiService.modifyDealWSHistoryData();
	}
	public void modifyDealZGPayDtData() throws BaseException {
		prjQuatPrpsZGApiService.modifyDealZGPayDtData();
	}

	@Override
	public DataResultVO exportCheckChangeOurDataResult() throws BaseException {
		DataResultVO result = new DataResultVO();
		Map<String,List<String>> errMsg  = 	 this.prjQuatPrpsZGApiService.exportCheckChangeOurDataResult();
		//导出错误Excel信息
		String pdfName = "错误数据信息("+ DateUtils.getCurrentDateStr(DateUtils.YYYYMMDDHHMMSS)+")";
		//文件保存路径
		String filePath = PrjPrdPaySchMConstant.CODE_PAY_SCH_FILE_SAVE_PATH;
		String serverFilePath = FileUtil.getServerFilePath(filePath);
		serverFilePath = serverFilePath  + pdfName + ".xls";
		try {
			FileOutputStream out = new FileOutputStream(serverFilePath);
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFFont headfont = wb.createFont();
			headfont.setFontName("宋体");
			headfont.setFontHeightInPoints((short) 22);// 字体大小
			HSSFCellStyle headstyle = wb.createCellStyle();
			headstyle.setFont(headfont);
			headstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
			headstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
			headstyle.setLocked(true);

			HSSFSheet sheet = wb.createSheet("错误数据");
			HSSFCellStyle style = wb.createCellStyle();
			style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
			style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
			// 普通单元格样式（中文）
			HSSFFont font2 = wb.createFont();
			font2.setFontName("宋体");
			font2.setFontHeightInPoints((short) 12);
			style.setFont(font2);

			HSSFRow titleRow = sheet.createRow((int) 0);
			HSSFCell titleCell = titleRow.createCell(0);
			titleCell.setCellValue("错误数据信息");
			titleCell.setCellStyle(headstyle);
			HSSFRow row = sheet.createRow((int) 1);
			HSSFCell cell = row.createCell(0);
			cell.setCellValue("合同号/支付表号");
			cell.setCellStyle(style);

			cell = row.createCell(1);
			cell.setCellValue("错误描述");
			cell.setCellStyle(style);
			int rowNum = 2;
			for (Map.Entry<String, List<String>> entry : errMsg.entrySet()) {
				List<String> valList = entry.getValue();
				if(entry.getValue().size()>0){
					for (int i = 0; i < valList.size(); i++) {
						HSSFRow schRow = sheet.createRow((int) i + rowNum);

						HSSFCell schCell = schRow.createCell(0);
						schCell.setCellValue(entry.getKey().toString());
						schCell.setCellStyle(style);

						schCell = schRow.createCell(1);
						schCell.setCellValue(valList.get(i).toString());
						schCell.setCellStyle(style);
					}
					rowNum+=valList.size();
				}
			}
			wb.write(out);
			out.flush();
			out.close() ;
			serverFilePath = serverFilePath.replace(PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_ROOT_PATH),
					PropertiesCacheUtils.getProperty(SysUlFilesRecConstant.SYS_FILE_UPLOAD_SERVER_PATH));
			logger.info("导出错误数据,返回服务器路径.serverPath:{}",serverFilePath);
			result.setData(serverFilePath);
			result.setInfo("导出成功");
			result.setSuccess(BaseConstant.IS_YESNO_YES);
		}catch (BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			logger.error("导出错误数据失败，msg:{}",ex.getMessage());
		}catch (Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			logger.error("导出错误数据失败,系统异常,msg:{}",ex.getMessage());
		}


		return  result;
	}

    @Override
    public void modifyDealZYChangeOurData() {
        prjQuatPrpsZGApiService.modifyDealZYChangeOurData();
    }

}
